﻿using FinalPointSet.Model.Asset;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Util;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;

namespace FinalPointSet.View
{
    /// <summary>
    /// StartView.xaml 的交互逻辑
    /// </summary>
    public partial class StartView : Window
    {
        public static readonly NotifyIcon notifyIcon = new();
        public StartView()
        {
            InitializeComponent();
            StringResourceManager.Instance.Init(System.Windows.Application.Current.Resources);
            bool isPowerBoot = Settings.Default.IsPowerBoot;
            PowerBootUtil.SetPowerBoot(isPowerBoot);

            //获取资源名称
            string appName = StringResourceManager.Instance.Get("APP_Name");
            string menuSearchViewName = StringResourceManager.Instance.Get("APP_NotifyIcon_Menu_SearchView_Name");
            string menuMainViewName = StringResourceManager.Instance.Get("APP_NotifyIcon_Menu_MainView_Name");
            string menuExitName = StringResourceManager.Instance.Get("APP_NotifyIcon_Menu_Exit_Name");
            //获取版本号
            Assembly assembly = Assembly.GetExecutingAssembly();
            string version = assembly.GetName().Version?.ToString() ?? string.Empty;
            //托盘
            NotifyIcon notifyIcon = new()
            {
                Icon = new Icon(AssetUtil.GetAbsolutePath(Path.APP_Logo)),
                Visible = true,
                Text = $"{appName} {version}"
            };

            //托盘的菜单
            ContextMenuStrip menu = new();
            ToolStripMenuItem showMenuItem = new(menuSearchViewName);
            showMenuItem.Click += (s, args) => WindowUtil.Instance.Show(typeof(SearchView), Location.Center);
            ToolStripMenuItem MainMenuItem = new(menuMainViewName);
            MainMenuItem.Click += (s, args) => WindowUtil.Instance.Show(typeof(MainView), Location.Center, ScaleMode.Auto);
            ToolStripMenuItem exitMenuItem = new(menuExitName);
            exitMenuItem.Click += (s, args) => WindowUtil.Instance.CloseAll();
            menu.Items.AddRange(new ToolStripItem[] {
                showMenuItem
                ,MainMenuItem
                ,exitMenuItem
            });
            //绑定托盘菜单
            notifyIcon.ContextMenuStrip = menu;
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            Debug.Print("StartView.Window_Closed,窗体关闭");
            Debug.Print("StartView.Window_Closed,注销全局热键");
            HotKeyManager.Instance.UnRegistHotKey(HotKeyManager.SearchView);
            HotKeyManager.Instance.UnRegistHotKey(HotKeyManager.MainView);
            notifyIcon?.Dispose();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //注册本身
            WindowUtil.Instance.Add(this, GetType());
            //注册热键
            Debug.Print("StartView.OnSourceInitialized,注册全局热键");
            HotKeyManager.Instance.RegistHotkey(HotKeyManager.SearchView);
            HotKeyManager.Instance.RegistHotkey(HotKeyManager.MainView);
            //后台运行自身
            WindowUtil.Instance.Hide(GetType());
            //默认唤醒搜索界面
            WindowUtil.Instance.SetWindowDesignSize(typeof(SearchView), new SizeF(Config.SearchWindowDesignWidth, Config.SearchWindowDesignHeight));
            WindowUtil.Instance.Show(typeof(SearchView), Location.Center);
        }
    }
}
